********************************************************************************
* Descriptive Statistics
* Tables: Firm and Employee Characteristics
********************************************************************************

* Load and prepare data
use "$data\4_individual_ano_vars.dta", clear

merge m:1 id wave using  "$data\2_firm_regressions_individual_paper.dta", ///
    keepusing(empcat knowledge_baseline)  
keep if _merge == 3
drop _merge

keep if wave == 1 & employer == 0

********************************************************************************
* TABLE 1: Baseline Values by Employee Characteristics
********************************************************************************

gen old = (status==1)

*-------------------------------------------------------------------------------
* Panel A: Gender
*-------------------------------------------------------------------------------
eststo clear

* Get counts
qui count if male == 0
local n1 = r(N)
qui count if male == 1
local n2 = r(N)

* Generate statistics and create table
eststo a1: quietly estpost summarize aminwage wrcon sosec empquality_ind if male == 0 
eststo a2: quietly estpost summarize aminwage wrcon sosec empquality_ind if male == 1

esttab a* using "$results\01_tables\Table_S9_employee_characteristics_outcomes.tex", ///
    cells(mean(fmt(%9.2f)) sd(par fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\begin{tabular}{l*{3}{c}}\hline" ///
        "\multicolumn{1}{l}{\textbf{Panel A: Gender}}" ///
        "&\multicolumn{1}{c}{Female}&\multicolumn{1}{c}{Male}&\multicolumn{1}{c}{ }\\") ///
    varlabels(aminwage "Above minimum wage" wrcon "Written contract" sosec "Social Security" empquality_ind "Formality Index") ///
    postfoot("\hline" "Observations        &         `n1'         &        `n2'         \\\hline \\") ///
    replace

*-------------------------------------------------------------------------------
* Panel B: Age
*-------------------------------------------------------------------------------
eststo clear

* Get counts
qui count if atleast30 == 0
local n1 = r(N)
qui count if atleast30 == 1
local n2 = r(N)

* Generate statistics and create table
eststo b1: quietly estpost summarize aminwage wrcon sosec empquality_ind if atleast30 == 0
eststo b2: quietly estpost summarize aminwage wrcon sosec empquality_ind if atleast30 == 1

esttab b* using "$results\01_tables\Table_S9_employee_characteristics_outcomes.tex", ///
    cells(mean(fmt(%9.2f)) sd(par fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\multicolumn{1}{l}{\textbf{Panel B: Age}}" ///
        "&\multicolumn{1}{c}{Younger than 30}&\multicolumn{1}{c}{30 or older}\\") ///
    varlabels(aminwage "Above minimum wage" wrcon "Written contract" sosec "Social Security" empquality_ind "Formality Index") ///
    postfoot("\hline" "Observations        &         `n1'         &        `n2'         \\\hline \\") ///
    append

*-------------------------------------------------------------------------------
* Panel C: Education
*-------------------------------------------------------------------------------
eststo clear

* Get counts
qui count if educ_tert == 0
local n1 = r(N)
qui count if educ_tert == 1
local n2 = r(N)

* Generate statistics and create table
eststo c1: quietly estpost summarize aminwage wrcon sosec empquality_ind if educ_tert == 0
eststo c2: quietly estpost summarize aminwage wrcon sosec empquality_ind if educ_tert == 1

esttab c* using "$results\01_tables\Table_S9_employee_characteristics_outcomes.tex", ///
    cells(mean(fmt(%9.2f)) sd(par fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\multicolumn{1}{l}{\textbf{Panel C: Education}}" ///
        "&\multicolumn{1}{c}{Less than tertiary}&\multicolumn{1}{c}{Tertiary}\\") ///
    varlabels(aminwage "Above minimum wage" wrcon "Written contract" sosec "Social Security" empquality_ind "Formality Index") ///
    postfoot("\hline" "Observations        &         `n1'         &        `n2'         \\\hline \\") ///
    append

*-------------------------------------------------------------------------------
* Panel D: Experience
*-------------------------------------------------------------------------------
eststo clear

* Get counts
qui count if exper6 == 0
local n1 = r(N)
qui count if exper6 == 1
local n2 = r(N)

* Generate statistics and create table
eststo d1: quietly estpost summarize aminwage wrcon sosec empquality_ind if exper6 == 0
eststo d2: quietly estpost summarize aminwage wrcon sosec empquality_ind if exper6 == 1

esttab d* using "$results\01_tables\Table_S9_employee_characteristics_outcomes.tex", ///
    cells(mean(fmt(%9.2f)) sd(par fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\multicolumn{1}{l}{\textbf{Panel D: Experience}}" ///
        "&\multicolumn{1}{c}{Up to 5 years}&\multicolumn{1}{c}{6+ years}\\") ///
    varlabels(aminwage "Above minimum wage" wrcon "Written contract" sosec "Social Security" empquality_ind "Formality Index") ///
    postfoot("\hline" "Observations        &         `n1'         &        `n2'         \\\hline \\") ///
    append

*-------------------------------------------------------------------------------
* Panel E: Supervisor Role
*-------------------------------------------------------------------------------
eststo clear

* Get counts
qui count if supervisor == 0
local n1 = r(N)
qui count if supervisor == 1
local n2 = r(N)

* Generate statistics and create table
eststo e1: quietly estpost summarize aminwage wrcon sosec empquality_ind if supervisor == 0
eststo e2: quietly estpost summarize aminwage wrcon sosec empquality_ind if supervisor == 1

esttab e* using "$results\01_tables\Table_S9_employee_characteristics_outcomes.tex", ///
    cells(mean(fmt(%9.2f)) sd(par fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\multicolumn{1}{l}{\textbf{Panel E: Supervisor Role}}" ///
        "&\multicolumn{1}{c}{No Supervisor}&\multicolumn{1}{c}{Supervisor}\\") ///
    varlabels(aminwage "Above minimum wage" wrcon "Written contract" sosec "Social Security" empquality_ind "Formality Index") ///
    postfoot("\hline" "Observations        &         `n1'         &        `n2'         \\\hline \\") ///
    append

*-------------------------------------------------------------------------------
* Panel F: Relationship to employer
*-------------------------------------------------------------------------------
eststo clear

* Get counts
qui count if relationship == 0
local n1 = r(N)
qui count if relationship == 1
local n2 = r(N)

* Generate statistics and create table
eststo f1: quietly estpost summarize aminwage wrcon sosec empquality_ind if relationship == 0
eststo f2: quietly estpost summarize aminwage wrcon sosec empquality_ind if relationship == 1

esttab f* using "$results\01_tables\Table_S9_employee_characteristics_outcomes.tex", ///
    cells(mean(fmt(%9.2f)) sd(par fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\multicolumn{1}{l}{\textbf{Panel F: Relationship to employer}}" ///
        "&\multicolumn{1}{c}{No relationship}&\multicolumn{1}{c}{Friends or Family}\\") ///
    varlabels(aminwage "Above minimum wage" wrcon "Written contract" sosec "Social Security" empquality_ind "Formality Index") ///
    postfoot("\hline" "Observations        &         `n1'         &        `n2'         \\\hline \\" "\end{tabular}") ///
    append

********************************************************************************
* TABLE 2: Baseline Values - All Employees Have Benefit (Employer Data)
********************************************************************************

* Load and prepare data  
use "$data\2_firm_regressions_individual_paper.dta", clear

* Generate max values by firm
foreach var in sh_aminwage_raw_2021 sh_wrcon_raw_2021 sh_sosec_raw_2021 {
    ren `var' `var'_temp
    bys id: egen `var' = max(`var'_temp)
    drop `var'_temp
}

keep if wave == 1

* Create benefit categories
label define benefit 0 "No employee has benefit" 1 "Some employees have benefit" 2 "All employees have benefit"

foreach var in sh_aminwage_raw_2021 sh_wrcon_raw_2021 sh_sosec_raw_2021 {
    gen dis_`var' = 0 if `var' == 0 
    replace dis_`var' = 1 if `var' > 0 & `var' < 1
    replace dis_`var' = 2 if `var' >= 1 & !missing(`var')
    label val dis_`var' benefit
}

* Display distributions
ta dis_sh_aminwage_raw_2021
ta dis_sh_wrcon_raw_2021
ta dis_sh_sosec_raw_2021

* Generate statistics
foreach var in aminwage wrcon sosec {
    estpost tabulate dis_sh_`var'_raw_2021
    eststo `var'
}
* Create table
esttab aminwage wrcon sosec using "$results\01_tables\Table_S10_descriptives_distribution_benefits.tex", ///
    cells(pct(fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\begin{tabular}{l*{4}{c}}\hline\hline" ///
        "&\multicolumn{1}{c}{Minimum Wage}&\multicolumn{1}{c}{Written Contract}&\multicolumn{1}{c}{Social Security}&\multicolumn{1}{c}{ }\\\hline") ///
    postfoot("\hline\end{tabular}") ///
    replace
   
 
********************************************************************************
* TABLE 3: Baseline Values by Firm Characteristics Using Employee Data
********************************************************************************

*-------------------------------------------------------------------------------
* Panel A: Firm Size
*-------------------------------------------------------------------------------
use "$data\4_individual_ano_vars.dta", clear
eststo clear

merge m:1 id wave using "$data\2_firm_regressions_individual_paper.dta", ///
    keepusing(empcat knowledge_baseline)  
keep if _merge == 3
drop _merge
keep if wave == 1 & employer == 0

* Generate statistics
eststo i1: quietly estpost summarize aminwage wrcon sosec empquality_ind if empcat == 1 
eststo i2: quietly estpost summarize aminwage wrcon sosec empquality_ind if empcat == 2 
eststo i3: quietly estpost summarize aminwage wrcon sosec empquality_ind if empcat == 3 

* Create table
esttab i* using "$results\01_tables\Table_S13_descriptives_firm_size_firm_knwoledge.tex", ///
    cells(mean(fmt(%9.2f)) sd(par fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\begin{tabular}{l*{4}{c}}\hline\hline" ///
        "\multicolumn{1}{l}{\textbf{Panel A: Number of Staff}}" ///
        "&\multicolumn{1}{c}{1-3 employees}&\multicolumn{1}{c}{4-6 employees}&\multicolumn{1}{c}{6+ employees}&\multicolumn{1}{c}{ }\\\hline") ///
    varlabels(aminwage "Above minimum wage" wrcon "Written contract" sosec "Social Security" empquality_ind "Formality Index") ///
    postfoot("\hline" "Observations        &         `n1'         &        `n2'         &        `n3'         \\\hline \\") ///
    replace

*-------------------------------------------------------------------------------
* Panel B: Knowledge
*-------------------------------------------------------------------------------
eststo clear

* Get counts
qui count if knowledge_baseline == 1
local n1 = r(N)
qui count if knowledge_baseline == 2
local n2 = r(N)
qui count if knowledge_baseline == 3
local n3 = r(N)

* Generate statistics
eststo k1: quietly estpost summarize aminwage wrcon sosec empquality_ind if knowledge_baseline == 1 
eststo k2: quietly estpost summarize aminwage wrcon sosec empquality_ind if knowledge_baseline == 2 
eststo k3: quietly estpost summarize aminwage wrcon sosec empquality_ind if knowledge_baseline == 3 

* Create table
esttab k* using "$results\01_tables\Table_S13_descriptives_firm_size_firm_knwoledge.tex", ///
    cells(mean(fmt(%9.2f)) sd(par fmt(%9.2f))) ///
    collabels(none) ///
    noobs nonumber nomtitle ///
    style(tex) ///
    prehead("\multicolumn{1}{l}{\textbf{Panel B: Employer Baseline Knowledge}}" ///
        "&\multicolumn{1}{c}{No knowledge}&\multicolumn{1}{c}{Moderate knowledge}&\multicolumn{1}{c}{High knowledge}&\multicolumn{1}{c}{ }\\\hline") ///
    varlabels(aminwage "Above minimum wage" wrcon "Written contract" sosec "Social Security" empquality_ind "Formality Index") ///
    postfoot("\hline" "Observations        &         `n1'         &        `n2'         &        `n3'         \\\hline \\" "\end{tabular}") ///
    append
